package com.itfreer.special.report;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itfreer.utils.ResponseEntity;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/reportcontent")
@Api("报表数据内容")
public class RestReportTableService {

	@Autowired(required = false)
	private ReportDictionaryService service;

	@Reference(lazy = true, check = false)
	private ReportDictionaryService dubboService;

	protected ReportDictionaryService getService() {
		if (service == null) {
			return dubboService;
		} else {
			return service;
		}
	}

	@PostMapping(value = { "/getEntitys" }, produces = { "application/json" })
	public ResponseEntity<Map<String, Object>> getEntitysByTable(@RequestBody Map<String, Object> map) {
		Map<String, Object> mapReturn = new HashMap<>();
		String id = "";
		String areaitem = "";
		String tableWhere = "";
		if (map.get("id") != null) {
			id = String.valueOf(map.get("id"));
		}
		if (map.get("area") != null && !"".equals(map.get("area"))) {
			areaitem = String.valueOf(map.get("area"));
			String[] areaList = areaitem.split(",");
			String wsql = "";
			for (int i=0;i<areaList.length;i++) {
				if(i!= areaList.length-1){
					wsql += "'" + areaList[i] + "',";
				}else{
					wsql += "'" + areaList[i] + "'";
				}
			}
			//政区排序
			tableWhere = " where areaid in (" + wsql + ") order by POSITION (areaid in '" + areaitem + "')";
		}
		ReportDictionaryEntity reportDictionaryEntity = getService().getEntity(id.toString());
		if (reportDictionaryEntity != null) {
			mapReturn.put("title", reportDictionaryEntity.getDataname());
			mapReturn.put("tableHead", reportDictionaryEntity.getTableHead());
			// 获取字段列表
			List<Map<String, Object>> listField = getService().getTableField(reportDictionaryEntity.getDatatable());
			mapReturn.put("headCount", listField.size());
			// 获取合并字段
			String mergefield = reportDictionaryEntity.getMergefield();
			String[] fieldList = null;
			if (mergefield != null && !"".equals(mergefield)) {
				fieldList = mergefield.split(",");
			}
			mapReturn.put("mergefield", fieldList);
			String otherWhere = "";
			//合并字段排序
			if(listField.size()>0) {
				for (int i=0;i<fieldList.length;i++) {
					otherWhere += "," + listField.get(Integer.valueOf(fieldList[i]));
				}
				if(!"".equals(tableWhere)) {
					tableWhere += otherWhere;
				}else {
					tableWhere += " order by " + otherWhere.substring(1);
				}
			}
			// 获取数据列表
			List<LinkedHashMap<String, Object>> list = getService().getTableList(reportDictionaryEntity.getDatatable(),
					"*", tableWhere);
			mapReturn.put("datalist", list);
		}
		return new ResponseEntity<Map<String, Object>>(mapReturn);
	}

	@PostMapping(value = { "/getCounts" }, produces = { "application/json" })
	public ResponseEntity<Long> getCounts(@RequestBody Map<String, Object> where) throws IOException {
		return new ResponseEntity<Long>(0L);
	}

	@PostMapping(value = { "/getFields" }, produces = { "application/json" })
	public ResponseEntity<Map<String, Object>> getFields(@RequestBody String datatable) throws IOException {
		Map<String, Object> mapReturn = new HashMap<>();
		if (datatable != null && !"".equals(datatable)) {
			List<Map<String, Object>> listField = getService().getTableField(datatable);
			mapReturn.put("fields", listField);
		}
		return new ResponseEntity<Map<String, Object>>(mapReturn);
	}
}
